// pages/user-message/user-message.js
var app = getApp();
Page({
  data: {
    currentTab: 0,
    pageSize:10, //每页数量
    pageNum: 1, //页数
    userId: '', //医生id
    dataList: [], //数据列表
    name: "",
    msgId: "", // 互动提醒消息id
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  
    pageName: 'message', // 控制tabbar图标
    isshowadd: 0, // 控制是否显示添加按钮
    num:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userId: wx.getStorageSync('userid')
    });
    this.getDataListByTab();
  },
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current,
      dataList:[]
    });
    that.getDataListByTab();
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
    }
  },
  // 获取信息
  getDataListByTab: function() {
    let {
      currentTab,
      pageSize,
      pageNum,
      userId
    } = this.data;
    if (currentTab == 0) {
      this.getList('doctor/usermsg/getMsgByUser', "POST", {
        pageSize: pageSize,
        pageNum: pageNum,
        userId: userId
      });
    }
    else if (currentTab == 1){
      this.getList('system/newsapi/getnewsbystate', "POST", {
        pageSize: pageSize,
        pageNum: pageNum,
        doctorId: userId,
        type : 0
      });
    }
    else if (currentTab == 2) {
      this.getList('system/newsapi/getnewspage', "POST", {
        pageSize: pageSize,
        pageNum: pageNum,
        doctorId: userId,
        type : 0
      });
    }
    else if (currentTab == 3) {
      this.getList(`shopp/orderNews/getOrderNewsPage?pagenum=${pageNum}&pagesize=${pageSize}`, "GET");
    }
    else if (currentTab == 4) {
      this.getList('consult/talkapi/gettalkpage', "POST", {
        pageSize: pageSize,
        pageNum: pageNum,
        type: 2
      });
    }
  },
  // 获取信息封装
  getList(url, method, params={}) {
    const that = this;
    app.showLoading();
    app.request({
      url: url,
      data: params,
      method: method,
      success: function(res) {
        wx.hideLoading();
      
        that.setData({
          dataList: res.data.orderNews || res.data,
          num:res.page.count
        })
      },
      fail: function(res) {
        // app.requestFail(res);
      }
    });
  },

  //订单跳转 1是术前术后  2是医美
  jop: function(e) {
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var orderId = e.currentTarget.dataset.orderid;
    if (orderType == 1) {
      wx.navigateTo({
        url: '../user-my-order-details/user-my-order-details?id=' + orderId + '&orderType=' + orderType,
      })
    } else {
      wx.navigateTo({
        url: '../user-my-order-details-medical-1/user-my-order-details-medical-1?id=' + orderId + '&orderType=' + orderType,
      })
    }
  },

  //删除互动提醒
  deleteClick: function(event) {
    var that = this;
    var userId = event.currentTarget.dataset.userid
    var msgId = event.currentTarget.dataset.id
    const param = {
      userId,
      msgId
    };
    app.request({
      url: "doctor/usermsg/deleteMsg",
      data: param,
      success: function(res) {
        if (res.status_code == 200) {
          app.showToast("删除成功")
          that.onLoad();
        } else {
          app.showToast("删除失败")
        }
      },
      fail: function(res) {
        // app.requestFail(res);
      }
    });
  },
  // 互动提醒详情
  entryDetail(e){
    const msgType = e.currentTarget.dataset.msgtype;
    const msgObj = e.currentTarget.dataset.msgobj;
    const businessId = e.currentTarget.dataset.businessid;
    const bookId = e.currentTarget.dataset.bookid;
    let url;
    if (msgType == 3){
      url = '../doctor-my-follows/doctor-my-follows'
    }else{
      if (msgObj == 3){
        url = '../doctor-review-details/doctor-review-details?id=' + businessId
      } else if (msgObj == 1){
        url = '../user-diary/user-diary?bookid=' + bookId + '&diaryId=' + businessId
      } else if (msgObj == 4){
        url = '../doctor-programmeDetail/doctor-programmeDetail?businessId=' + businessId
      }else{
        url = '../doctor-my-case-details/doctor-my-case-details?id=' + businessId + '&isshowadd=' + this.data.isshowadd
      } 
    }
    wx.navigateTo({
      url: url
    })
  },

  // 删除订单
  deleteorder: function(e) {
    const that = this;
    const id = e.currentTarget.dataset.id
    const url = 'shopp/orderNews/deleteOrderNews/' + id;
    app.request({
      url: url,
      method: "GET",
      success: function(res) {
        wx.showToast({
          title: "成功",
          icon: 'success',
          duration: 1000
        })
        that.getDataListByTab();
      },
      fail: function(res) {
        wx.showToast({
          title: '请求失败',
          icon: 'none',
          duration: 1000
        })
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  bottomAdd: function (e) {
    
    let { currentTab, num, userId, dataList, pageSize} = this.data;
    const that = this;
   
    // app.showLoading();
    console.log(num);
    console.log(dataList.length)
   
    this.setData({
      pageSize: pageSize + 10
    })
    if (dataList.length == num) {
      console.log("num")
      wx.showToast({
        title: '没有更多了哦~',
        icon: 'none',
        duration: 1000
      });
    } 
    else {
      if (currentTab == 0) {
        this.getList('doctor/usermsg/getMsgByUser', "POST", {
          pageSize: that.data.pageSize,
          pageNum: that.data.pageNum,
          userId: userId
        });
      }
      if (currentTab == 1) {
        this.getList('system/newsapi/getnewsbystate', "POST", {
          pageSize: that.data.pageSize,
          pageNum: that.data.pageNum,
          doctorId: userId,
          type: 0
        });
      }
      if (currentTab == 2) {
        this.getList('system/newsapi/getnewspage', "POST", {
          pageSize: that.data.pageSize,
          pageNum: that.data.pageNum,
          doctorId: userId,
          type: 0
        });
      }
      if (currentTab == 3) {
        this.getList(`shopp/orderNews/getOrderNewsPage?pagenum=${that.data.pageNum}&pagesize=${that.data.pageSize}`, "GET");
      }
      if (currentTab == 4) {
        this.getList('consult/talkapi/gettalkpage', "POST", {
          pageSize: that.data.pageSize,
          pageNum: that.data.pageNum,
          type: 2
        });
      }
    }
  },
})